SaltyPaws RPI Setup

Part 3: Protecting the RPi4

Bentley Smith

S/V SaltyPaws

OVERVIEW & PURPOSE

Issue: the Raspberry Pi (RPi4) doesn't like to have its power cut while running. This can lead to corruption of the SD card.

Goal #1: put the RPi4 in a safe state when we're finished using it, so that if a power interruption does occur, it will not be harmful. {Note: “shutdown” for RPi4 really means reboot partially, then stop and wait for command to proceed further, stopping at a safe spot. “Shutdown”, “Sleep”, and “Turn Off” are used interchangeably.}

Goal #2: allow the RPi4 to easily wake up (complete the reboot.)

This requires intervention with both software and hardware.

Software

There are many different references for different ways to turn on/off the RPi4.

Please reference https://github.com/lihak/rpi-power-button for discussion and installation of this software component. This allows the RPi4 to turn on using GPIO 3 and off using GPIO 24.

If you are using MPU9255 sensors, or any other HAT on the GPIO pin 3, this software will wake the RPi4 just fine because the I2 C is also sleeping.

Follow the code instructions from the website (clone, then run setup)

Additionally:

I wanted to control an LED, to indicate when the RPi4 was in a running state. In terminal:

sudo nano /home/pi/.bashrc

Add line:

sudo python /home/pi/led.py

ctrl-x, save

sudo nano led.py
import time
GPIO.setmode(GPIO.BCM)
LED = 21
GPIO.setup(LED, GPIO.OUT)
GPIO.output(LED,True)
time.sleep(1)

ctrl-x, save

chmod +x led.py

Hardware

Wiring to the GPIO pins is straightforward, just remember that GPIO numbers and physical pin numbers are different.

GPIO 3 = pin 5

GPIO 24 = pin 18

GPIO 21 = pin 40

raspberrypi4gpio-02_resize_69.jpg

Since we are using MPU9255, which covers pin 5, I soldered a wire to the top of the MPU9255 where the pin connects.

All this software on/off can be run with an external momentary switch. One press the RPi4 turns on, next press it turns off.

In order to make life easier, I wanted to have the touchscreen, nav instruments, AIS and RPi4 all respond to the same switch, which on our boat is the breaker on the power panel.

To make this happen, I had to construct an electronic momentary switch that responded to the power panel switched 12v power, using relays and capacitors. I even threw in LEDs to show the current status.

Relays (3), 12v coil, one with NO/NC contacts. I used SRD-12VDC-SL-C

Capacitors (2) 330µF-1000µF the higher the capacity, the longer the “momentary” button push.

LED (3) 12v.

img_20210205_162521.jpg

In my LED color scheme the LEDs will indicate if the nav instruments and touchscreen are on (green) or off (red). The purple LED shows when the RPi4 is awake.

Results

Switching on the nav instruments and touchscreen activates the relay on the left, causing 12v power to the green LED and to the capacitor until the capacitor saturates, then that current flow stops. When that current is flowing, it closes the right upper relay to act like a button push for GPIO 3. This turns the RPi4 on, and pin 40, GPIO 21, powers the purple LED.

Switching off the 12v power causes the red LED to receive power, and current to flow in the lower right relay, until the capacitor is saturated, acting as a momentary switch for GPIO 24, causing the RPi4 to shutdown, and stop powering the purple LED.